<?php
/* SVN FILE: $Id: fw24h_controller.php 49 2009-08-26 07:02:47Z phamshantuyet@gmail.com $ */
/**
*
* @author $Author: phamshantuyet@gmail.com $
* @version $Revision: 49 $
* @lastrevision $Date: 2009-08-26 14:02:47 +0700 (Wed, 26 Aug 2009) $
* @modifiedby $LastChangedBy: phamshantuyet@gmail.com $
* @lastmodified $LastChangedDate: 2009-08-26 14:02:47 +0700 (Wed, 26 Aug 2009) $
* @filesource $URL: https://ladybirdphp.googlecode.com/svn/trunk/website/fw24h2/fw24h_controller.php $
*/

class FW24H_Controller {
	
	var $layout = 'default';
	var $masterContent = '';
	var $viewVars = array();
	var $actionName = '';
	var $controllerName = '';
	var $pageTitle = '';
	var $beforeFilter = array();
	var $afterFilter = array();
	var $useModel = array();
	var $useComponent = array();
	var $useHelpers = array();
	var $blockNoCache = false;
	var $blockNoCacheArray = array();
	var $_path_info = array();
	var $_args = array();

	var $afterRender = array();

	var $block_cache_timeout = 1800;
	/**
	 * Ham duoc goi truoc khi action chinh duoc thuc thi
	 *
	 */
	/**
	 * FW24H_Controller::beforeFilter()
	 * 
	 * @return
	 */
	function beforeFilter() {

	}
	
	/**
	 * Ham duoc goi sau khi action chinh duoc thuc thi
	 *
	 */
	/**
	 * FW24H_Controller::afterFilter()
	 * 
	 * @return
	 */
	function afterFilter() {
	
	}

	/**
	 * Gan gia tri su dung cho template
	 *
	 * @param string $key
	 * @param mixed $value
	 */
	/**
	 * FW24H_Controller::setParam()
	 * 
	 * @param mixed $key
	 * @param mixed $value
	 * @return
	 */
	function setParam( $key, $value) {
		$this->viewVars[$key] = $value;
	}

	/**
	 * Hien thi noi dung toan bo website
	 *
	 */
	/**
	 * FW24H_Controller::displayView()
	 * 
	 * @return
	 */
	function displayView() {
		extract( $this->viewVars);
		$masterContent = $this->masterContent;
		//ob_start();                    // Start output buffering
		include( APP_DIR.'/views/layouts/'.$this->layout.'.php');  // Include the file
		//$contents = ob_get_contents(); // Get the contents of the buffer
		//ob_end_clean();                // End buffering and discard
		//return $contents;  
	}

	/**
	 * Xu ly giao dien ung voi action hien tai
	 *
	 * @param string $viewAction file giao dien can hien thi, neu ko co tu dong lay ten file giao dien trung voi ten action
	 */
	/**
	 * FW24H_Controller::render()
	 * 
	 * @param string $viewAction
	 * @return
	 */
	function render( $viewAction = '') {
		if( $viewAction) {
			$view = APP_DIR.'/views/'.strtolower($this->controllerName).'/'.$viewAction.'.php';
		}else{
			$view = APP_DIR.'/views/'.strtolower($this->controllerName).'/'.$this->actionName.'.php';
		}
		extract( $this->viewVars);
		ob_start();  
		include( $view);  // Include the file
		$this->masterContent = ob_get_contents(); // Get the contents of the buffer
		ob_end_clean();                // End buffering and discard
	}

	/**
	 * Constructor
	 *
	 * @return FW24H_Controller
	 */
	/**
	 * FW24H_Controller::FW24H_Controller()
	 * 
	 * @return
	 */
	function FW24H_Controller() {
		
	}

	/**
	 * Nhan lai gia tri da gan de su dung o template
	 *
	 * @param string $key
	 * @return mixed
	 */
	/**
	 * FW24H_Controller::getParam()
	 * 
	 * @param mixed $key
	 * @return
	 */
	function getParam( $key){
	    if( isset($this->viewVars[$key]))
	    {
            return $this->viewVars[$key];
	    }
		return null;
	}
	
	/**
	 * Chuyen den 1 trang khac
	 *
	 * @param string $p_link
	 */
	/**
	 * FW24H_Controller::redirect()
	 * 
	 * @param mixed $p_link
	 * @return
	 */
	function redirect( $p_link, $p_header=true) {
		//@ob_end_clean();
		if( $p_header || headers_sent()) {
			echo '<script language="JavaScript">location.href="'.$p_link.'"</script><meta http-equiv="Refresh" content="0;URL='.$p_link.'" />';
			exit();
		}
		header('location: '.$p_link);
		exit();
	}

	/**
	 * Ham duoc goi o trong giao dien, dung de xu ly 1 element dung chung
	 *
	 * @param unknown_type $p_element
	 */
	/**
	 * FW24H_Controller::renderElement()
	 * 
	 * @param mixed $p_element
	 * @return
	 */
	function renderElement( $p_element) {
		extract( $this->viewVars);
		include_once APP_DIR.'/views/elements/'.$p_element.'.php';
	}

	function loadModel( $model, $model_name='', $model_path='') {
		if( $model_path!='') {
			require_once $model_path.strtolower( $model).'_model.php';
		} else {
			$m = strtolower( $model);
			require_once APP_DIR.'/modules/'.$m.'/models/'.$m.'_model.php';
		}

		$modelName = ucfirst( $model);
		if( $model_name!='') {
			$modelName = $model_name;
		}
		$modelClass = $model.'Model';
		$this->$modelName = new $modelClass();
	}

	function loadHelper( $helperClass, $path=null) {
		$out = strtolower( str_replace( 'Helper', '_helper', $helperClass));
		if( $path) {
			require_once $path.'/'.$out.'.php';
		}else {
			require_once APP_DIR.'/views/helpers/'.$out.'.php';
		}
		$this->setParam( str_replace( 'Helper', '', $helperClass), new $helperClass);
	}

	function __loadHelpers() {
		if(isset($this->useHelpers) && is_array($this->useHelpers)){
	 		foreach($this->useHelpers as $helperClass){	
				if(preg_match('/(\w+)Helper/', $helperClass, $out)){
					require_once APP_DIR.'/views/helpers/'.$this->underscore($out[0]).'.php';
	 				if( class_exists($out[0]) ){ 
						$this->setParam( ucfirst($out[1]), new $out[0] ); 	
	 				}
	 			}
	 		}
	 	}
	}

	function underscore($camelCase) {
		return strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCase));
	}

	public function afterRender() {
		
	}
}



//-----------

